'use strict';

var auth = angular.module('authApp', ['ngCookies', 'authctrl']);
angular.module('authctrl', ['authsvc']);
angular.module('authsvc', []);

angular.module('authctrl').controller('authcontroller', function($scope, $cookieStore, identitysvc) {
    $scope.identity = function(nickname) {
        identitysvc.identity(nickname).then(function(response) {
            var valueJson = JSON.stringify(response);
            var valueObject = JSON.parse(valueJson);
            // console.log(valueObject);

            var status = valueObject.status;
            if (status == "200") {
                var user = valueObject.data.user;
                if (angular.isUndefined(user.id)) {

                } else {
                    var id = user.id;
                    window.sessionStorage.setItem("userId", id);
                    window.sessionStorage.setItem("userNameToken", user.name);
                    $cookieStore.put("userObm", user);
                    $cookieStore.put("menuObm", valueObject.data.menu);
                    $cookieStore.put("session", valueObject.data.sessionId);
                    $scope.username = user.name;
                }
            }
        });
    }
});

auth.value('server', {
    // host:'23.1.1.173',
    host: '192.168.3.233',
    getUrl: function(data) {
        return 'http://' + this.host + ':9002/services/auth/sso/';
    }
});



angular.module('authsvc')
    .factory('identitysvc', function($rootScope, $http, $q, $timeout, server) {
        return {
            identity: function(nickname) {
                server.getUrl('user') + nickname;
                return $http.get(server.getUrl('user') + nickname);
            }
        };
    });